import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ScrollAndStack {

	private static Shell shell;
	private static int currentCompositeNumber = 0;
	private static Composite0 composite0;
	private static Composite1 composite1;
	private static Composite stack;
	private static ScrolledComposite scroll;

	public ScrollAndStack() {
		scroll = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
		scroll.setExpandHorizontal(true);
		scroll.setExpandVertical(true);
		stack = new Composite(scroll, SWT.BORDER);
		stack.setLayout(new StackLayout());
		composite0 = new Composite0(stack, SWT.BORDER);
		composite1 = new Composite1(stack, SWT.BORDER);
		StackLayout layout = (StackLayout) stack.getLayout();
		layout.topControl = composite0;
		scroll.setContent(stack);
		scroll.setMinSize(stack.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		stack.layout();
	}

	public static void main(String[] args) {
		Display display = new Display();
		shell = new Shell(display);
		shell.setLayout(new GridLayout(1,false));
		Button switchButton = new Button(shell, SWT.PUSH);
		switchButton.setText("Switch");
		switchButton.setLayoutData(new GridData());
		switchButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				if (currentCompositeNumber == 0) {
					StackLayout layout = (StackLayout) stack.getLayout();
					layout.topControl = composite1;
					currentCompositeNumber = 1;
				} else {
					StackLayout layout = (StackLayout) stack.getLayout();
					layout.topControl = composite0;
					currentCompositeNumber = 0;
				}
				scroll.setMinSize(stack.computeSize(SWT.DEFAULT, SWT.DEFAULT));
				stack.layout();
			}
		});
		new ScrollAndStack();
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	class Composite0 extends Composite {
		public Composite0(Composite parent, int style) {
			super(parent, style);
			setLayout(new GridLayout());
			Text text;
			for (int i = 0; i < 5; i++) {
				text = new Text(this, SWT.NONE);
				text.setText("Composite0 Text " + i);
				text.setLayoutData(new GridData());
			}
		}
	}

	class Composite1 extends Composite {
		public Composite1(Composite parent, int style) {
			super(parent, style);
			setLayout(new GridLayout());
			Text text;
			for (int i = 0; i < 30; i++) {
				text = new Text(this, SWT.NONE);
				text.setText("Composite1 Text " + i);
				text.setLayoutData(new GridData());
			}
		}
	}
}
